package com.xgc.erp.order.purchase.ctrl;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xgc.common.base.BaseController;
import com.xgc.common.config.Constan;
import com.xgc.common.model.PurchaseDetail;
import com.xgc.common.vo.Feedback;
import com.xgc.common.vo.Grid;
import com.xgc.erp.order.purchase.service.PurchaseDetailService;


import java.util.ArrayList;
import java.util.Date;

//@ControllerBind(path = "/erp/order/purchaseDetail")
public class PurchaseDetailController extends BaseController {
    @Inject
    PurchaseDetailService detailService;

    public void index(){
        String orderId = get("orderId");
        set("orderId",orderId);
        render("index.html");
    }
    /**
     * 订单明细
     */
    public void list(){
        String orderId = get("orderId");
        if(orderId==null || orderId.equals("")){
            renderJson(new Grid(null, getParaToInt("pageNumber",1), getParaToInt("pageSize",10),0));
            return;
        }
        set("orderId",orderId);         // 页面隐藏框值填充
        Record record = new Record();
        record.set("order_id",orderId);
        String sql = Db.getSql("erp.getPurchaseDetailsByOrderId");
        Grid grid = detailService.queryForListEq(sql,getParaToInt("pageNumber",1), getParaToInt("pageSize",10),record,"order by create_time desc");
        renderJson(grid);
    }

    public void add(){
        Integer orderId = getInt("orderId");
        set("purchaseDetail",new PurchaseDetail().setOrderId(orderId));
        render("add.html");
    }

    public void save(){
        PurchaseDetail purchaseDetail=getBean(PurchaseDetail.class);
        purchaseDetail.setDetailId(detailService.getPrimaryKey(Constan.SEQ_PURCHASE_DETAIL));
        purchaseDetail.setCreator(getVisitor().getCode());
        purchaseDetail.setCreateTime(new Date());
        if(!detailService.save(purchaseDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("add.html");
    }

    public void edit(){
        String detailId = get("detailId");
        PurchaseDetail purchaseDetail = (PurchaseDetail) detailService.findById(detailId);
        setAttr("purchaseDetail",purchaseDetail);
        render("edit.html");
    }

    public void update(){
        PurchaseDetail purchaseDetail=getBean(PurchaseDetail.class);
        purchaseDetail.setModifier(getVisitor().getCode());
        purchaseDetail.setUpdateTime(new Date());
        if(!detailService.save(purchaseDetail)){
            renderJson(Feedback.error("失败!"));
        }
        render("edit.html");
    }

}
